<template>
    <div>
        <!--          面包屑-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item >🏠</el-breadcrumb-item>
            <el-breadcrumb-item>血糖管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!--        卡片区域-->
        <el-card class="box-card">

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="success"
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                >删除</el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="sugarList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="用户名" align="center" prop="userName" />
            <el-table-column label="血糖值" align="center" prop="bloodGlucose" />
            <el-table-column label="记录时间" align="center" prop="time" width="180">
<!--                <el-date-picker clearable size="small" style="width: 200px"-->
<!--                                v-model="form.time"-->
<!--                                type="date"-->
<!--                                value-format="yyyy-MM-dd"-->
<!--                                placeholder="选择记录时间">-->
<!--                </el-date-picker>-->
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['system:sugar:edit']"
                    >修改</el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['system:sugar:remove']"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

            <!--     分页     -->
            <el-pagination
                    v-show="total>0"
                    :page-size="queryParams.pageSize"
                    :pager-count="11"
                    :current-page.sync="queryParams.pageNum"
                    :total="total"
                    @current-change="getList"
            >
            </el-pagination>

        <!-- 添加或修改blood_sugar对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="用户名" prop="userName">
                    {{form.userName}}
                </el-form-item>
                <el-form-item label="血糖值" prop="bloodGlucose">
                    <el-input v-model="form.bloodGlucose" placeholder="请输入血糖值" />
                </el-form-item>
                <el-form-item label="记录时间" prop="time">
                    <el-date-picker clearable size="small" style="width: 200px"
                                    v-model="form.time"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="选择记录时间">
                    </el-date-picker>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        </el-card>
    </div>

</template>

<script>

    export default {
        name: "xuetang",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // blood_sugar表格数据
                sugarList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    bloodGlucose: null,
                    time: null,
                    userId: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询blood_sugar列表 */
            getList() {
                this.loading = true;
                this.loading = true;
                const token = sessionStorage.getItem("token");
                this.$http.defaults.headers.common['Authorization'] = token;
                this.$http.get("/bloodSugar/list?pageNum="+this.queryParams.pageNum).then(response=>{
                    console.log(response);
                    this.sugarList = response.data.data.bloodSugar;
                    console.log(this.sugarList);
                    this.total = response.data.data.total;
                    this.loading = false;
                });

            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    userName: null,
                    bloodGlucose: null,
                    time: null,
                    userId: null
                };
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.form = row;
                this.open = true;
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            // this.form.time = this.form.date1 + " "+this.form.date2;
                            console.log(this.form);
                            console.log(this.form.time);
                            const token = sessionStorage.getItem("token");
                            this.$http.defaults.headers.common['Authorization'] = token;
                            console.log(this.form)
                            this.$http.put('bloodSugar', this.form).then(response => {
                                if (response.data.code === 200) {
                                    this.$message({showClose: true, message: "修改成功", type: "success"})
                                    this.open = false;
                                    this.getList()
                                }
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除id编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    const token = sessionStorage.getItem("token");
                    this.$http.defaults.headers.common['Authorization'] = token;
                    this.$http.delete(`bloodSugar/${ids}`)
                    this.getList();
                    this.$message("删除成功");
                });
            },
        }
    };
</script>
